import os
import socket
import threading
class chat_server():
    def __init__(self,host,port,outtime=100,listen=5):
        self.server=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
        self.server.bind((host,port))
        self.server.listen(listen)
        self.users_data={'wr440':'84906440'}
        self.clients=[]
    def run(self):#开始运行
        while True:
            client,addr=self.server.accept()
            self.clients.append(client)
            threading.Thread(target=self.__send_msg,args=(client,)).start()
    def __send_msg(self,client):#接收用户消息然后广播
        while True:
            try:
                msg=client.recv(1024)
            except:
                self.clients.remove(client)
                client.close()
                break
            for c in self.clients:
                if c==client:
                    pass
                else:
                    c.send(msg)
def main():
    host='192.168.0.151'
    port=9999
    cs=chat_server(host,port)
    cs.run()
if __name__=='__main__':
    main()


